package com.example.cunion.controller;

import cn.hutool.core.bean.BeanUtil;
import com.example.cunion.common.R;
import com.example.cunion.config.shiro.JwtUtil;
import com.example.cunion.controller.form.*;
import com.example.cunion.entity.Coupon;
import com.example.cunion.service.CouponService;
import com.example.cunion.util.StringSnowflakeIdGenerator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/coupon")
public class CouponController {

    @Resource
    private CouponService couponService;

    @Resource
    private JwtUtil jwtUtil;

    @PostMapping("/searchUnCheckCouponByPage")
    public R searchUnCheckCouponByPage(@RequestHeader("token") String token, @RequestBody SearchUnCheckCouponByPageForm form){
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        List<HashMap> list = couponService.searchUnCheckCouponByPage(map);
        return R.ok().put("result", list);
    }

    @GetMapping("/agreeCoupon")
    public R agreeCoupon(@RequestHeader("token") String token, @RequestParam("id") String id, @RequestParam("shopId") String shopId){
        Integer result = couponService.agreeCoupon(id, shopId);
        return R.ok();
    }

    @GetMapping("/rejectCoupon")
    public R rejectCoupon(@RequestHeader("token") String token, @RequestParam("id") String id){
        Integer result = couponService.rejectCoupon(id);
        return R.ok();
    }

    @PostMapping("/addCoupon")
    public R addCoupon(@RequestHeader("token") String token, @RequestBody AddCouponForm form){
        StringSnowflakeIdGenerator generator = new StringSnowflakeIdGenerator(1, 1);
        String id = generator.nextId();
        form.setId(id);
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = couponService.addCoupon((HashMap) map);
        return R.ok();
    }

    @PostMapping("/searchBusinessCheckByPage")
    public R searchBusinessCheckByPage(@RequestHeader("token") String token, @RequestBody SearchBusinessCheckByPageForm form){
        String userId = jwtUtil.getUserId(token);
        Integer start = form.getStart();
        Integer length = form.getLength();
        start = (start - 1) * length;
        HashMap<String, Object> map = new HashMap<>();
        map.put("start", start);
        map.put("length", length);
        map.put("userId", userId);
        List<HashMap> list = couponService.searchBusinessCheckByPage(map);
        return R.ok().put("result", list);
    }

    @PostMapping("/updateCouponStatus")
    public R updateCouponStatus(@RequestHeader("token") String token, @RequestBody UpdateCouponStatusForm form){
        Map<String, Object> map = BeanUtil.beanToMap(form);
        Integer result = couponService.updateCouponStatus((HashMap) map);
        return R.ok();
    }


    @PostMapping("/addCouponByMp")
    public R addCouponByMp(@RequestHeader("token") String token, @RequestBody Coupon coupon) {
        couponService.addCoupon(coupon);
        return R.ok();
    }

    @GetMapping("/deleteCouponByMp")
    public R deleteCouponByMp(@RequestHeader("token") String token, @RequestParam("couponId") String couponId) {
        couponService.deletedCoupon(couponId);
        return R.ok();
    }
    @PostMapping("/updateCouponByMp")
    public R updateCouponByMp(@RequestHeader("token") String token, @RequestBody Coupon coupon) {
        couponService.updateCoupon(coupon);
        return R.ok();
    }
    @PostMapping("/searchCouponByMp")
    public R searchCouponByMp(@RequestHeader("token") String token, @RequestBody PageFom form){
        List<Coupon> list = couponService.searchCoupon(form.getStart(), form.getLength());
        return R.ok().put("result", list);
    }
}
